package com.hna.face.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

public class HttpClientUtil {

	/**
	 * 
	 * Get请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */
	public static String get(String url, List<NameValuePair> params,
			List<NameValuePair> headerParams) {
		HttpClient hc = new DefaultHttpClient();
		String body = null;
		try {
			// Get请求
			HttpGet httpget = new HttpGet(url);
			// 设置参数
			if (params != null && params.size() > 0) {
				String str = EntityUtils.toString(new UrlEncodedFormEntity(
						params));
				httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
			}
			// httpget.setHeader(HTTP.CONTENT_TYPE,
			// "application/json;charset=UTF-8");
			// if (headerParams != null) {
			// for (NameValuePair param : headerParams) {
			// if (param != null && param.getName() != null
			// && param.getValue() != null) {
			// httpget.setHeader(param.getName(), param.getValue());
			// }
			// }
			// }
			// 发送请求
			HttpResponse httpresponse = hc.execute(httpget);
			httpresponse.setHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						httpresponse.setHeader(param.getName(),
								param.getValue());
					}
				}
			}
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			body = EntityUtils.toString(entity);
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		return body;
	}

	/**
	 * 
	 * // Post请求
	 * 
	 * @param url
	 * 
	 * @param params
	 * 
	 * @return
	 */

	public static String post(String url, String sendBody) {
		return post(url, sendBody, null);
	}

	public static String post(String url, String sendBody,
			List<NameValuePair> headerParams) {
		HttpClient httpClient = new DefaultHttpClient();
		String body = null;
		try {
			// Post请求
			HttpPost post = new HttpPost(url);
			StringEntity se = new StringEntity(sendBody, "UTF-8");
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json"));
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8"));
			post.setEntity(se);
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						post.setHeader(param.getName(), param.getValue());
					}
				}
			}
			try {
				// 发送请求
				HttpResponse httpresponse = httpClient.execute(post);
				if(httpresponse.getStatusLine().getStatusCode()!=200){
					return null;
				}
				// 获取返回数据
				HttpEntity entity = httpresponse.getEntity();
				// //得到实体输入流
				// InputStream inSm = entity.getContent();
				// Scanner inScn = new Scanner(inSm);
				// while(inScn.hasNextLine()){
				// System.out.println(inScn.nextLine());
				// }

				body = EntityUtils.toString(entity);
				if (entity != null) {
					entity.consumeContent();
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} /*
		 * catch (UnsupportedEncodingException e) { e.printStackTrace(); }
		 */catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpClient.getConnectionManager().shutdown();
		return body;
	}

	public static String postXML(String url, String sendBody) {
		return postXML(url, sendBody, null);
	}

	public static String postXML(String url, String sendBody,
			List<NameValuePair> headerParams) {
		HttpClient httpClient = new DefaultHttpClient();
		String body = null;
		try {
			// Post请求
			HttpPost post = new HttpPost(url);
			StringEntity se = new StringEntity(sendBody, "UTF-8");
//			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
//					"application/xml;charset=UTF-8"));
			post.setEntity(se);
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						post.setHeader(param.getName(), param.getValue());
					}
				}
			}
			try {
				// 发送请求
				HttpResponse httpresponse = httpClient.execute(post);
				// 获取返回数据
				HttpEntity entity = httpresponse.getEntity();
				// //得到实体输入流
				// InputStream inSm = entity.getContent();
				// Scanner inScn = new Scanner(inSm);
				// while(inScn.hasNextLine()){
				// System.out.println(inScn.nextLine());
				// }

				body = EntityUtils.toString(entity);
				if (entity != null) {
					entity.consumeContent();
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} /*
		 * catch (UnsupportedEncodingException e) { e.printStackTrace(); }
		 */catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpClient.getConnectionManager().shutdown();
		return body;
	}

	/**
	 * 
	 * @param url
	 * @param sendBody
	 * @return
	 */
	public static String put(String url, String sendBody) {
		return post(url, sendBody, null);
	}

	public static String put(String url, String sendBody,
			List<NameValuePair> headerParams) {
		HttpClient httpClient = new DefaultHttpClient();
		String body = null;
		try {
			// Put请求
			HttpPut httpput = new HttpPut(url);
			StringEntity se = new StringEntity(sendBody, "UTF-8");
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8"));
			httpput.setEntity(se);
			if (headerParams != null) {
				for (NameValuePair param : headerParams) {
					if (param != null && param.getName() != null
							&& param.getValue() != null) {
						httpput.setHeader(param.getName(), param.getValue());
					}
				}
			}
			try {
				// 发送请求
				HttpResponse httpresponse = httpClient.execute(httpput);
				// 获取返回数据
				HttpEntity entity = httpresponse.getEntity();
				// //得到实体输入流
				// InputStream inSm = entity.getContent();
				// Scanner inScn = new Scanner(inSm);
				// while(inScn.hasNextLine()){
				// System.out.println(inScn.nextLine());
				// }

				body = EntityUtils.toString(entity);
				if (entity != null) {
					entity.consumeContent();
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} /*
		 * catch (UnsupportedEncodingException e) { e.printStackTrace(); }
		 */catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpClient.getConnectionManager().shutdown();
		return body;
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		// List<NameValuePair> params = new ArrayList<NameValuePair>();
		//
		// params.add(new BasicNameValuePair("email", "xxx@gmail.com"));
		//
		// params.add(new BasicNameValuePair("pwd", "xxx"));
		//
		// params.add(new BasicNameValuePair("save_login", "1"));

		// login hiwCW/4iWApmUiet12SaYTq7+uaSV9Tt
		// String url1 = "http://192.168.200.202:8080/open_rest/login";
		// String body1 = post(
		// url1,
		// "{\"userid\":\"13716914943\",\"pass\":\"dVGzdEyMgQ9Ng==\",\"language\":\"zh_CN\",\"version\":\"1.01\",\"logintype\":\"-1\"}");
		// System.out.println(body1);

		// register
		// String url2 = "http://192.168.200.202:8080/open_rest/users";
		// String body2 = post(
		// url2,"{\"mobile\":\"13716914943\",\"pass\":\"dVGzdEyMgQ9Ng==\",\"language\":\"zh_CN\",\"version\":\"1.01\",\"username\":\"中国人\",\"nickname\":\"大熊\",\"address\":\"大熊洞里过冬天\"}");
		// System.out.println(body2);

		// activate
		// String url3 = "http://192.168.200.202:8080/open_rest/users/activate";
		// String body3 = post(
		// url3,"{\"code\":\"302715\"}");
		// System.out.println(body3);

		// userinfo
		// String url4 = "http://192.168.200.202:8080/open_rest/users";
		// List<NameValuePair> headerParams = new ArrayList<NameValuePair>();
		// headerParams.add(new BasicNameValuePair("session",
		// "JEx0BaYh1qdI4Ri33LzhnJU4u9yZEVXY"));
		// String body4 = get(url4, null, headerParams);
		// System.out.println(body4);

		String url3 = "http://192.168.200.202/hsjuhome/webservice/userauth/register";
		String body3 = postXML(
				url3,
				"<?xml version='1.0' encoding='UTF-8'?><register><email>liuxd12223mail@sohu.com</email><pass>123456==</pass></register>");
		System.out.println(body3);
		String body4 = postXML(
				url3,
				"<?xml version='1.0' encoding='UTF-8'?><register><email>liuxd12322456mail@sohu.com</email><pass>123456==</pass></register>");
		System.out.println(body4);
	}

}
